package cn.hwadee.muying.commons.utils;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Mankind on 2018/4/17.
 */
public class DateTools {

    public static final SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    public static final SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
    public static final SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat hourFormat = new SimpleDateFormat("yyyy-MM-dd HH");
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat showMonthFormat = new SimpleDateFormat("yyyy-MM");
    public static final SimpleDateFormat showDayFormat = new SimpleDateFormat("MM-dd");
    public static final SimpleDateFormat showHourFormat = new SimpleDateFormat("HH:mm");
    public static final long DAYMILLS = 1000*3600*24;

    /*计算两个时间戳之间的天数*/
    public static long betweetDays(Timestamp a, Timestamp b){return (b.getTime() - a.getTime()) / DAYMILLS;}

    /*计算距离下次生日的天数*/
    public static long nextBirthdayDays(Date date)
    {
        Calendar today = Calendar.getInstance();
        Calendar birthday = Calendar.getInstance();
        birthday.setTime(date);
        birthday.set(Calendar.YEAR, today.get(Calendar.YEAR));
        long days;
        if (birthday.get(Calendar.DAY_OF_YEAR) < today.get(Calendar.DAY_OF_YEAR)) {
            // 生日已经过了，要算明年的了
            days = today.getActualMaximum(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR);
            birthday.set(Calendar.YEAR, today.get(Calendar.YEAR) + 1);
            days += birthday.get(Calendar.DAY_OF_YEAR);
        } else {
            // 生日还没过
           days = birthday.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR);
        }
        return days;
    }

    /*计算start到end的工作日天数*/
    public static long getWorkDay(Date startDate,Date endDate)
    {
        Locale.setDefault(Locale.CHINA);
        Calendar x = Calendar.getInstance();
        Calendar y = Calendar.getInstance();
        x.setTime(startDate);
        y.setTime(endDate);
        int days = 0;
        while (x.getTime().compareTo(y.getTime()) <= 0) {
            if (x.get(Calendar.DAY_OF_WEEK) != 6 && x.get(Calendar.DAY_OF_WEEK) != 7){
                days++;
            }
            x.add(Calendar.DAY_OF_MONTH,1);
        }
        return days;
    }

    /*计算start到end的工作日天数*/
    public static long getDays(Date startDate,Date endDate)
    {
        Locale.setDefault(Locale.CHINA);
        Calendar x = Calendar.getInstance();
        Calendar y = Calendar.getInstance();
        x.setTime(startDate);
        y.setTime(endDate);
        int days = 0;
        while (x.getTime().compareTo(y.getTime()) <= 0) {
            days++;
            x.add(Calendar.DAY_OF_MONTH,1);
        }
        return days;
    }
    /**
     * 获取当前时间戳
     * @return
     */
    public static Timestamp getTime(){
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * 获取当前时间往前推半个小时,并且精确到小时的时间戳
     * @return
     */
    public static Timestamp getTimeExactToHour(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        //calendar.add(Calendar.MINUTE,-30);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);//精确到小时
        Timestamp curDate=new Timestamp(calendar.getTimeInMillis());
        return curDate;
    }

    /**
     * 获取当前时间往前推半个小时,并且精确到天的时间戳
     * @return
     */
    public static Timestamp getTimeExactToDay(Date date){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        //calendar.add(Calendar.MINUTE,-30);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);//精确到天
        Timestamp curDate=new Timestamp(calendar.getTimeInMillis());
        return curDate;
    }


    /**
     * 获取当前时间往前推半个小时,并且精确到月的时间戳
     * @return
     */
    public static Timestamp getTimeExactToMonth(Date date){
        //calendar.add(Calendar.MINUTE,-30);
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);//精确到月
        Timestamp curDate=new Timestamp(calendar.getTimeInMillis());
        return curDate;
    }

    /**
     * 获取当前时间往前推半个小时,并且精确到年的时间戳
     * @return
     */
    public static Timestamp getTimeExactToYear(){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MINUTE,-30);
        calendar.set(Calendar.MONTH,0);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);//精确到年
        Timestamp curDate=new Timestamp(calendar.getTimeInMillis());
        return curDate;
    }


}
